package pers.xx.sbm.satisfy;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import pers.xx.sbm.dao.DQuestionAnswerMapper;
import pers.xx.sbm.dao.QuestionAnswerIndexMapper;
import pers.xx.sbm.dao.StudentQuestionMapper;
import pers.xx.sbm.entity.QuestionAnswerIndex;
import pers.xx.sbm.entity.StudentQuestion;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author ：xiexing
 * @description：分数转换 学生
 * @date ：2022/1/18 14:05
 */
@SpringBootTest
public class ScoreJuniorTransfer {
    @Autowired
    DQuestionAnswerMapper dQuestionAnswerMapper;


    @Autowired
    QuestionAnswerIndexMapper questionAnswerIndexMapper;

    @Autowired
    StudentQuestionMapper studentQuestionMapper;

    @Test
    void getQeustion() throws Exception {
//        count(221, "小学");
//        count(222, "初中");
//        count(223, "高中");
    }

    private void count(int questionnaireId, String xd) throws Exception {
        List<StudentQuestion> studentQuestionList = studentQuestionMapper.getQuestions(xd);
        List<Map<String, String>> questionAnswers = dQuestionAnswerMapper.getQuestionAnswers("学生", questionnaireId);//获取答题人
        List<QuestionAnswerIndex> list = scoreTransfer(studentQuestionList, questionAnswers, xd);
        questionAnswerIndexMapper.insertBatch(list);
    }


    /**
     * @description 分数转化
     * @author XieXing
     * @date 2022/1/19 9:28
     */
    public List<QuestionAnswerIndex> scoreTransfer(List<StudentQuestion> studentQuestionList, List<Map<String, String>> questionAnswers, String xd) throws Exception {
        List<QuestionAnswerIndex> questionAnswerIndexList = new ArrayList<>();
        for (Map<String, String> obj : questionAnswers) {
            String respondentNo = obj.get("respondent_no");//学生唯一标识
            String schoolName = obj.get("school_name");//学校
            String countyName = obj.get("area_code");//区县名称
            String questionnaireName = ("2021学生问卷（" + xd + "）");//问卷名称
            Object sex = obj.get("sex");
            for (int i = 1; i <= 25; i++) {
                String answer = obj.get(i + "");
                QuestionAnswerIndex questionAnswerIndex = new QuestionAnswerIndex();
                StudentQuestion studentQuestion = studentQuestionList.get(i - 1);
                questionAnswerIndex.setIndexName(studentQuestion.getIndexName());
                questionAnswerIndex.setAreaName(countyName);
                questionAnswerIndex.setQuestionnaireName(questionnaireName);
                questionAnswerIndex.setRespondent("学生");
                questionAnswerIndex.setRespondentNo(respondentNo);
                questionAnswerIndex.setSchoolName(schoolName);
                questionAnswerIndex.setXd(xd);
                questionAnswerIndex.setSex(Integer.valueOf((Integer) sex));
                if (answer.isEmpty() || answer.equals("#")) {
                    questionAnswerIndex.setIndexScore(1);//没选的按1分处理
                } else {
                    String scoreType = studentQuestion.getScoreType().trim();
                    if ("正向计分".equals(scoreType)) { //正向计分
                        if (answer.equals("A")) {
                            questionAnswerIndex.setIndexScore(1);
                        } else if (answer.equals("B")) {
                            questionAnswerIndex.setIndexScore(2);
                        } else if (answer.equals("C")) {
                            questionAnswerIndex.setIndexScore(3);
                        } else if (answer.equals("D")) {
                            questionAnswerIndex.setIndexScore(4);
                        } else if (answer.equals("E")) {
                            questionAnswerIndex.setIndexScore(5);
                        } else {
                            questionAnswerIndex.setIndexScore(1);
                        }
                    } else if ("反向计分".equals(scoreType)) { //反向计分
                        if (answer.equals("A")) {
                            questionAnswerIndex.setIndexScore(5);
                        } else if (answer.equals("B")) {
                            questionAnswerIndex.setIndexScore(4);
                        } else if (answer.equals("C")) {
                            questionAnswerIndex.setIndexScore(3);
                        } else if (answer.equals("D")) {
                            questionAnswerIndex.setIndexScore(2);
                        } else if (answer.equals("E")) {
                            questionAnswerIndex.setIndexScore(1);
                        } else {
                            questionAnswerIndex.setIndexScore(1);
                        }
                    }
                }
                questionAnswerIndexList.add(questionAnswerIndex);
                if (questionAnswerIndexList.size() % 60001 == 0) {
                    questionAnswerIndexMapper.insertBatch(deepCopy(questionAnswerIndexList));
                    questionAnswerIndexList.clear();
                }
            }//循环题
        }//循环人
        return questionAnswerIndexList;
    }

    /**
     * @description 深度拷贝
     * @author XieXing
     * @date 2022/1/19 15:36
     */
    public static List deepCopy(List<QuestionAnswerIndex> src) throws IOException,
            ClassNotFoundException {
        ByteArrayOutputStream byteout = new ByteArrayOutputStream();
        ObjectOutputStream out = new ObjectOutputStream(byteout);
        out.writeObject(src);
        ByteArrayInputStream bytein = new ByteArrayInputStream(byteout
                .toByteArray());
        ObjectInputStream in = new ObjectInputStream(bytein);
        List<QuestionAnswerIndex> dest = (List<QuestionAnswerIndex>) in.readObject();
        return dest;
    }

}
